package com.yyx.lgb.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.yyx.lgb.manager.service.ValidateCodeService;
import com.yyx.lgb.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: ValidateCodeServiceImpl
 * Package: com.yyx.lgb.manager.service.impl
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/7/31 - 14:29
 */
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    //获取图片验证码
    @Override
    public ValidateCodeVo generateValidateCode() {
        //使用hutool工具类生成验证码
        //参数：宽，高，验证码数字 干扰线数量
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 2);
        String codeValue = circleCaptcha.getCode();
        String imageBase64 = circleCaptcha.getImageBase64();

        //uuid作为验证码的key
        String codeKey = UUID.randomUUID().toString().replace("-", "");

        //存储到redis
        redisTemplate.opsForValue().set("user:validateCode"+codeKey,codeValue,5, TimeUnit.MINUTES);

        //获得响应结果数据
        ValidateCodeVo vo = new ValidateCodeVo();
        vo.setCodeKey(codeKey);
        vo.setCodeValue("data:imge/png;base64,"+imageBase64);
        return vo;
    }
}
